
local ColorUtility = require("UnityEngine.ColorUtility")
local TankManager = class("TankManager", require("unity.LuaMonoBehaviour"))

function TankManager:constructor(c) 
    self.m_PlayerColor = c.m_PlayerColor
    self.m_SpawnPoint = c.m_SpawnPoint
    self.m_PlayerNumber = 0
    self.m_ColoredPlayerText = nil
    self.m_Instance = nil
    self.m_Wins = 0
    self.m_Movement = nil
    self.m_Shooting = nil
    self.m_CanvasGameObject = nil
end

function TankManager:Setup() 
    self.m_Movement = self.m_Instance:GetLuaComponent("TankMovement")
    self.m_Shooting = self.m_Instance:GetLuaComponent("TankShooting")
    self.m_CanvasGameObject = self.m_Instance:GetComponentInChildren(tolua.typeof(UnityEngine.Canvas)).gameObject
    self.m_Movement.m_PlayerNumber = self.m_PlayerNumber
    self.m_Shooting.mPlayerNumber = self.m_PlayerNumber
    self.m_ColoredPlayerText = "<color=#" .. ColorUtility.ToHtmlStringRGB(self.m_PlayerColor) .. ">PLAYER " .. tostring(self.m_PlayerNumber) .. "</color>"
    local renderers = self.m_Instance:GetComponentsInChildren(tolua.typeof(UnityEngine.MeshRenderer))
    for i,v in table.ipairs_Array(renderers) do
        v.material.color = self.m_PlayerColor
    end
end

function TankManager:DisableControl() 
    self.m_Movement.enabled = false
    self.m_Shooting.enabled = false
    self.m_CanvasGameObject:SetActive (false)
end

function TankManager:EnableControl() 
    self.m_Movement.enabled = true
    self.m_Shooting.enabled = true
    self.m_CanvasGameObject:SetActive (true)
end

function TankManager:Reset() 
    self.m_Instance.transform.position = self.m_SpawnPoint.position
    self.m_Instance.transform.rotation = self.m_SpawnPoint.rotation
    self.m_Instance:SetActive (false)
    self.m_Instance:SetActive (true)
end

return TankManager
